package com.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.SaleCartBean;
import com.project.bean.SaleOrderBean;
import com.project.bean.UserBean;
import com.project.service.IBuyCartService;
import com.project.service.ISaleCartService;
import com.project.service.ISaleDetailService;
import com.project.service.ISaleOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("saleCart")
@Api(tags = "销售车")
public class SaleCartController {
    @Autowired
    private ISaleCartService saleCartService;
    @Autowired
    private ISaleOrderService saleOrderService;
    @Autowired
    private ISaleDetailService detailService;

    @ApiOperation(value = "按登录用户查看购物车所有记录")
    @GetMapping("findById")
    public List<SaleCartBean> findById(HttpServletRequest request) {
        UserBean user = (UserBean) request.getSession().getAttribute("loginUser");
        return saleCartService.findById(user.getId());
    }

    @ApiOperation(value = "添加销售车信息")
    @PostMapping("add")
    public String add(String shopId,HttpServletRequest request) {
        UserBean user = (UserBean) request.getSession().getAttribute("loginUser");
        saleCartService.add(shopId,user.getId());
        return "ok";
    }

    @ApiOperation(value = "按ID删除销售车")
    @DeleteMapping ("del")
    public String del(Integer id) {
        saleCartService.del(id);
        return "ok";
    }

    @ApiOperation(value = "出库")
    @PostMapping("addOrder")
    public String add(String customName, String address, Integer totalPrice, HttpServletRequest request) {
        UserBean user = (UserBean) request.getSession().getAttribute("loginUser");
        //添加销售订单
        SaleOrderBean saleOrderBean = new SaleOrderBean();
        saleOrderBean.setSaleName(user.getName());
        saleOrderBean.setAddress(address);
        saleOrderBean.setTotalPrice(totalPrice);
        saleOrderBean.setCustomName(customName);
        saleOrderService.add(saleOrderBean);
        //按登陆用户，查询销售车集合
        saleCartService.findById(user.getId());
        //按销售ID、销售车集合，批量添加销售明细。同时累减库存数量
        detailService.addDetailList(saleOrderBean.getId(), user.getId());
        //按用户ID删除销售车记录
        saleCartService.delByUserId(user.getId());
        return "ok";
    }




}
